/*
	Name: 最优贸易
	Author: There,hello
	Date: 07-11-19 12:23
	Description: Luogu P1073
	Algorithm: tarjan缩点+Dp
*/
#include<bits/stdc++.h>
using namespace std;
#define add(x,y) edge[++tail]=(dd){head[x],y};head[x]=tail
#define add2(x,y) side[++tot]=(dd){head_[x],y};head_[x]=tot
int n,m;
int head[100005],head_[100005],tail,tot,val[100005],color[100005],
dfn[100005],low[100005],ti,color_tail,val_new[100005][2],in[100005];
int f[100005];
bool v[100005];
stack<int> sta;
struct dd{
    int ne,to;
}edge[1000055],side[1000005];
void dfs(int t){
    dfn[t]=low[t]=++ti;
    sta.push(t);
    v[t]=1;
    for(int i=head[t];i;i=edge[i].ne){
        if(!dfn[edge[i].to]){
            dfs(edge[i].to);
            low[t]=min(low[t],low[edge[i].to]);
        }
        else if(v[edge[i].to])low[t]=min(low[t],dfn[edge[i].to]);
    }
    if(dfn[t]!=low[t])return;
    int x=sta.top();
    ++color_tail;
    while(x!=t){
        color[x]=color_tail;
        v[x]=0;
        sta.pop();
        x=sta.top();
    }
    sta.pop();
    v[t]=0;
    color[t]=color_tail;
}
void ready(){
    for(int i=1;i<=color_tail;++i)
    val_new[i][0]=0x3f3f3f3f;
    for(int i=1;i<=n;++i){
        val_new[color[i]][0]=min(val[i],val_new[color[i]][0]);
        val_new[color[i]][1]=max(val[i],val_new[color[i]][1]);
    }
    for(int i=1;i<=n;++i){
        for(int j=head[i];j;j=edge[j].ne){
            if(color[i]!=color[edge[j].to]){
                add2(color[i],color[edge[j].to]);
                in[color[edge[j].to]]++;
            }
        }
    }
}
void toposort(){
    queue<int> que;
    for(int i=1;i<=color_tail;++i){
        if(in[i]==0){
            que.push(i);
            f[i]=val_new[i][1]-val_new[i][0];
        }
    }
    while (!que.empty())
    {
        int t=que.front();
        que.pop();
        for(int i=head_[t];i;i=side[i].ne){
            f[side[i].to]=max(max(f[side[i].to],val_new[side[i].to][1]-val_new[side[i].to][0]),
                            max(f[t],val_new[side[i].to][1]-val_new[t][0]));
            in[side[i].to]--;
            if(in[side[i].to]==0)que.push(side[i].to);
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)
    scanf("%d",&val[i]);
    for(int i=1,x,y,z;i<=m;++i){
        scanf("%d%d%d",&x,&y,&z);
        if(z==1){
            add(x,y);
        }
        else {
            add(x,y);
            add(y,x);
        }
    }
    dfs(1);
    ready();
    toposort();
    printf("%d",f[color[n]]);
    return 0;
}